package com.cmii.sjw.department.control.jsc.controller;

import com.cmii.sjw.department.control.jsc.model.dto.MenuQueryDTO;
import com.cmii.sjw.department.control.jsc.model.entity.Menu;
import com.cmii.sjw.department.control.jsc.model.vo.MenuNode;
import com.cmii.sjw.department.control.jsc.service.MenuService;
import com.cmii.sjw.department.control.jsc.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 菜单管理控制器
 * @Author jfh
 * @Date 2025-04-08 周二 15:35
 * @Version 1.0
 */
@Slf4j
@RestController
@CrossOrigin
@RequestMapping("/sys/menu")
public class MenuController {

    @Autowired
    private MenuService menuService;

    /**
     * 获取菜单树
     * @return 菜单树
     */
    @PostMapping("/tree")
    public Result<List<MenuNode>> getMenuTree() {
        List<MenuNode> menuTree = menuService.getMenuTree();
        return Result.success(menuTree);
    }

    /**
     * 保存菜单
     * @param menu 菜单信息
     * @return 保存结果
     */
    @PostMapping("/save")
    public Result saveMenu(@RequestBody Menu menu) {
        boolean success = menuService.saveMenu(menu);
        return success ? Result.success() : Result.error("保存菜单失败");
    }

    /**
     * 获取菜单列表
     * @return 菜单列表
     */
    @PostMapping("/list")
    public Result<List<Menu>> getMenuList() {
        List<Menu> menus = menuService.getMenuList();
        return Result.success(menus);
    }

    /**
     * 根据条件获取菜单列表
     * @param queryDTO 查询条件
     * @return 菜单列表
     */
    @PostMapping("/menuListByCondition")
    public Result<List<Menu>> menuListByCondition(MenuQueryDTO queryDTO) {
        List<Menu> menus = menuService.getMenuListByCondition(queryDTO);
        return Result.success(menus);
    }

    /**
     * 获取菜单详情
     * @param id 菜单ID
     * @return 菜单详情
     */
    @PostMapping("/detail")
    public Result<Menu> getMenuDetail(@RequestParam Long id) {
        Menu menu = menuService.getMenuById(id);
        return Result.success(menu);
    }

    /**
     * 删除菜单
     * @param id 菜单ID
     * @return 删除结果
     */
    @PostMapping("/delete")
    public Result deleteMenu(@RequestParam Long id) {
        boolean success = menuService.deleteMenu(id);
        return success ? Result.success() : Result.error("删除菜单失败");
    }
}
